--region
--Date
--此文件由[BabeLua]插件自动生成
print("----------------------------------chipmunk_test")

require("dump")
local cp = require("chipmunk")

print(type(cp), cp)
--for k, v in pairs(cp) do
--    print(k, v)
--end
--print(type(chipmunk), type(chipmunk.createSpace))
--local space = createSpace()
--local space = cpSpaceNew()
--local space = addShape()
--test()
--space:cpSpaceNew()

local space = cpSpaceNew()
cpSpaceSetGravity(space, cpv(0, -100))

        --创建一个静态平面
local ground = cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(-20, 0), cpv(20, 0), 0));
setFriction(ground, 1.0);

local body = cpBodyNew(1, 1)
print(type(body), body.cpBodySetPosition)
cpBodySetPosition(body, cpv(0, 15))
cpSpaceAddBody(space, body)
----
local shape = cpCircleShapeNew(body, 10, cpv(0,0))
setFriction(shape, 0.5)
cpSpaceAddShape(space, shape)
--
local printf = function(...)
    print(string.format(...))
end

-- 创建碰撞回调函数
local function onCollisionBegin(arbiter, space, _)
    -- 获取参与碰撞的两个物体
    local shapeA = arbiter:getShapes()
    local shapeB = arbiter:getShapes()

    -- 判断碰撞发生在哪个形状上
    if shapeA == shape1 or shapeB == shape1 then
        -- 处理shape1与其他物体的碰撞逻辑
    end

    if shapeA == shape2 or shapeB == shape2 then
        -- 处理shape2与其他物体的碰撞逻辑
    end
end
-- 获取或创建碰撞处理程序，并将碰撞回调函数添加到处理程序中
local handler = cpSpaceAddCollisionHandler(space, shape1:getCollisionType(), shape2:getCollisionType())
handler.beginFunc = onCollisionBegin


local timeStep = 1/60
for i=1, 300 do
    cpSpaceStep(space, 1/60)
    local pos = getPosition(body)
    local vel = getVelocity(body);
    printf("Time is %f. Ball is at (%f, %f). Velocity is (%f, %f)\n", i * timeStep, pos.x, pos.y, vel.x, vel.y);
end



--endregion
